using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Text;
using System.Windows.Forms;
namespace Lifter.Gui.Monitor.Controls
{
	public class CommunicationStateControl : BaseControl
	{
		public enum COMMUNICATION_STATE
		{
			DISCONNET,
			EQUIPMENT_OFFLINE,
			EQUIPMENT_GOING_ONLINE,
			HOST_OFFLINE,
			ONLINE_LOCAL,
			ONLINE_REMOTE
		}
		private Timer timer = new Timer();
		private int tickCount = 0;
		private CommunicationStateControl.COMMUNICATION_STATE communicationState = CommunicationStateControl.COMMUNICATION_STATE.DISCONNET;
		private int borderWith = 0;
		private bool isBlink = false;
		private int interval = 500;
		private IContainer components = null;
		public int BorderWith
		{
			get
			{
				return this.borderWith;
			}
			set
			{
				this.borderWith = value;
				base.Invalidate();
			}
		}
		public CommunicationStateControl.COMMUNICATION_STATE CommunicationState
		{
			get
			{
				return this.communicationState;
			}
			set
			{
				this.communicationState = value;
				base.Invalidate();
			}
		}
		public bool IsBlink
		{
			get
			{
				return this.isBlink;
			}
			set
			{
				this.isBlink = value;
				if (this.isBlink)
				{
					this.timer.Enabled = true;
				}
				else
				{
					this.timer.Enabled = false;
				}
				base.Invalidate();
			}
		}
		public int Interval
		{
			get
			{
				return this.interval;
			}
			set
			{
				this.interval = value;
				base.Invalidate();
			}
		}
		public CommunicationStateControl()
		{
			this.InitializeComponent();
			this.timer.Enabled = false;
			this.timer.Interval = 500;
			this.timer.Tick += new EventHandler(this.Timer_Tick);
		}
		protected override void OnPaint(PaintEventArgs e)
		{
			this.DrawBody(e.Graphics);
		}
		private void DrawBody(Graphics g)
		{
			if (g == null)
			{
				g = base.CreateGraphics();
			}
			g.SmoothingMode = SmoothingMode.HighQuality;
			g.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
			Brush brush = Brushes.White;
			Color fillColor = Color.FromArgb(85,85,85);
			string connectString = "";
			switch (this.communicationState)
			{
				case CommunicationStateControl.COMMUNICATION_STATE.DISCONNET:
					connectString = "Host Distconnect";
					brush = Brushes.Black;
					break;
				case CommunicationStateControl.COMMUNICATION_STATE.HOST_OFFLINE:
					connectString = "Offline";
					brush = Brushes.Red;
					break;
				case CommunicationStateControl.COMMUNICATION_STATE.ONLINE_LOCAL:
					connectString = "Local";
					brush = Brushes.Yellow;
					break;
				case CommunicationStateControl.COMMUNICATION_STATE.ONLINE_REMOTE:
					connectString = "Online";
					brush = Brushes.Lime;
					break;
			}
			Rectangle rect = new Rectangle(this.borderWith, this.borderWith, base.Width - this.borderWith * 2, base.Height - this.borderWith * 2);
			g.FillRectangle(new SolidBrush(fillColor),rect);
			Font font = new Font("Tahoma", 12f, FontStyle.Bold);
			StringFormat stringFormat = new StringFormat();
			stringFormat.Alignment = StringAlignment.Center;
			stringFormat.LineAlignment = StringAlignment.Center;
			g.DrawString(connectString, font, brush, new RectangleF(this.borderWith, this.borderWith, base.Width - this.borderWith * 2, base.Height - this.borderWith * 2), stringFormat);
		}
		private void Timer_Tick(object sender, EventArgs e)
		{
			if (this.tickCount == 1)
			{
				this.tickCount = 0;
				base.Invalidate();
			}
			else
			{
				this.tickCount = 1;
				base.Invalidate();
			}
		}
		protected override void Dispose(bool disposing)
		{
			if (disposing && this.components != null)
			{
				this.components.Dispose();
			}
			base.Dispose(disposing);
		}
		private void InitializeComponent()
		{
		}
	}
}
